package cn.chen.controller;

import cn.chen.model.Action;
import cn.chen.service.ActionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbpu
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/action")
public class ActionController {
    @Autowired
    private ActionService actionService;


    //增
    @PostMapping("/addAction")
    public boolean addAction(@RequestBody Action action){
        return actionService.save(action);
    }

    //删
    @PostMapping("/deleteAction")
    public boolean deleteAction(@RequestBody Action action){
        return actionService.removeById(action);
    }

    //改
    @PostMapping("/updateAction")
    public boolean updateAction(@RequestBody Action action){
        return actionService.updateById(action);
    }

    //查
    @GetMapping("/getAction")
    public Action getAction(Action action){
        return actionService.getById(action);
    }

    //查所有
    @GetMapping("/getAllAction")
    public Page<Action> getAllAction(Integer page, Integer size){
        return actionService.page(new Page<>(page,size));
    }

    //根据gid分页查评论
    @GetMapping("/getActionByVid")
    public List<Action> getActionByVid(Integer page, Integer size,Integer gid){
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getGid,gid);
      return   actionService.page(new Page<>(page,size),wrapper).getRecords();

    }

    //根据uid查评论
    @GetMapping("/getActionByUid")
    public List<Action> getActionByUid(Integer page, Integer size,Integer uid){
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid,uid);
        return actionService.page(new Page<>(page,size),wrapper).getRecords();
    }

    //根据uid和gid把查出的结果把每个的isCollect设置为1
    @GetMapping("/setIsCollect")
    public boolean setIsCollect(Integer uid,Integer gid){
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid,uid);
        wrapper.eq(Action::getGid,gid);
        List<Action> list = actionService.list(wrapper);
        for (Action action : list) {
            action.setIsCollect(1);
        }
        //更新
       return actionService.updateBatchById(list);
    }

    //取消收藏
    @GetMapping("/cancelCollect")
    public boolean cancelCollect(Integer uid,Integer gid){
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid,uid);
        wrapper.eq(Action::getGid,gid);
        List<Action> list = actionService.list(wrapper);
        for (Action action : list) {
            action.setIsCollect(0);
        }
        //更新
        return actionService.updateBatchById(list);
    }

    //根据评论内容模糊查询
    @GetMapping("/getActionByContent")
    public List<Action> getActionByContent(Integer page, Integer size,Integer uid,String content){
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Action::getContent,content);
        wrapper.eq(Action::getUid,uid);
        return actionService.page(new Page<>(page,size),wrapper).getRecords();
    }

    //获取收藏列表
    @GetMapping("/getCollectList")
    public List<Action> getCollectList(Integer page, Integer size,Integer uid){
        LambdaQueryWrapper<Action> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Action::getUid,uid);
        wrapper.eq(Action::getIsCollect,1);
        //筛选里面的重复的gid对应的数据
        wrapper.groupBy(Action::getGid);
        return actionService.page(new Page<>(page,size),wrapper).getRecords();
    }







}
